package com.aio.news.home.fragment;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aidi.lib.TAInjectView;
import com.aio.news.R;
import com.aio.news.SwipeBackBaseFragment;
import com.aio.news.home.adapter.NewsDetailAdapter;
import com.aio.news.model.NewsDetailModel;
import com.aio.news.net.HttpUtils.Method;
import com.aio.news.net.HttpUtils.RequestState;
import com.aio.news.utils.StringUtil;
import com.aio.news.view.DetailMenuView;
import com.aio.news.view.DetailMoreView;
import com.squareup.picasso.Picasso;

public class NewsDetailFragment extends SwipeBackBaseFragment implements View.OnClickListener{

	@TAInjectView(id=R.id.listview)
	private ListView mListView;
	@TAInjectView(id=R.id.detail_bottom)
	private RelativeLayout bottomView;
	@TAInjectView(id=R.id.detail_layout_top)
	private RelativeLayout topView;
	@TAInjectView(id=R.id.menu_layout)
	private DetailMenuView menuView;
	@TAInjectView(id=R.id.more_view)
	private DetailMoreView moreView;
	
	private RequestState mState = new RequestState("detail/news");
	
	private TextView title;
	private TextView comed;
	private TextView date;
	private ImageView img;
	
	private NewsDetailAdapter mAdapter;
	
	@Override
	public View initView(LayoutInflater inflater) {
		return inflater.inflate(R.layout.news_detail_layout, null);
	}

	@Override
	public List<NameValuePair> getParams(RequestState state) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("id", mState.tid));
		return params;
	}

	@Override
	public void RequestSuccess(String response, RequestState state) {
		if(!TextUtils.isEmpty(response)){
			NewsDetailModel model = mGson.fromJsonWithNoException(response, NewsDetailModel.class);
			if(model != null && model.mItem != null){
				if(!TextUtils.isEmpty(model.mItem.title)){
					title.setText(model.mItem.title);
				}
				if(!TextUtils.isEmpty(model.mItem.comed)){
					comed.setText(model.mItem.comed);
				}
				if(model.mItem.time>0){
					date.setText(StringUtil.getDateToStringLong(model.mItem.time));
				}
				if(!TextUtils.isEmpty(model.mItem.url_big)){
					img.setVisibility(View.VISIBLE);
					Picasso.with(getActivity()).load(model.mItem.url_big).into(img);
				}
				if(!model.mItem.description.isEmpty()){
					mAdapter.setMessage(model.mItem.description);
				}else{
					mAdapter.notifyDataSetChanged();
				}
			}
		}
		mState.isRequestServer = false;
	}

	@Override
	public void RequestFail(String errString, RequestState state) {
		mState.isRequestServer = false;
	}

	@Override
	public void afterCreated() {
		mState.tid = getArguments().getString("id");
		View header = View.inflate(getActivity(), R.layout.news_detail_header_layout, null);
		title = (TextView)header.findViewById(R.id.title);
		comed = (TextView)header.findViewById(R.id.comed);
		date = (TextView)header.findViewById(R.id.date);
		img = (ImageView)header.findViewById(R.id.image);
		mListView.addHeaderView(header);
		mAdapter = new NewsDetailAdapter(this);
		mListView.setAdapter(mAdapter);
		initClick(bottomView);
		initClick(topView);
		getMessage(mState, Method.GET);
	}
	
	private void initClick(RelativeLayout layout){
		for(int i=0;i<layout.getChildCount();i++){
			layout.getChildAt(i).setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_top:
		case R.id.back:
			back();
			break;
		case R.id.share_top:
		case R.id.share:
			break;
		case R.id.setting_top:
		case R.id.setting:
			moreView.openOrClose();
			break;
		default:
			break;
		}
	}

}
